home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
motif
/
obuffer.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
10KB
|
362 lines
/*
* Copyright (c) 1993-94, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*----------------------------------------------------------------------------
*
* obuffer : openGL (motif) demo to switch between single/double buffer mode
*
* Author : Yusuf Attarwala
* SGI - Applications
* Date : Jan 93
*
* note : the main intent of this program is to demo the buffer
* switching functionality, hence the rendering is kept
* simple (wireframe).
*
* press left button for single buffer
* middle button for double buffer
* right button for animation
*
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <Xm/Xm.h>
#include <Xm/Frame.h> /* for frame widgets */
#include <X11/keysym.h> /* keyboard translations */
#include <X11/StringDefs.h>
#include <GL/gl.h> /* gl includes */
#include <GL/glu.h> /* utility library includes */
#include <GL/GLwMDrawA.h> /* include for the drawing area widget */
/* function declarations */
void
createToplevel(void),
createSingleBuffer(void),
createDoubleBuffer(void),
toSingleBuffer(void),
toDoubleBuffer(void),
animation(void),
drawScene(void),
setMatrix(void),
exposeCB(Widget,XtPointer,XtPointer),
resizeCB(Widget,XtPointer,XtPointer),
initCB(Widget,XtPointer,XtPointer),
inputCB(Widget,XtPointer,XtPointer);
/* global variables */
Display *display; /* current display */
XtAppContext appContext; /* X application context */
int doubleBuffer; /* flag for current buffer status */
float ax,ay,az; /* angles for animation */
GLUquadricObj *quadObj; /* used in drawscene */
Widget toplevel, /* toplevel shell */
frameSB, /* frame to hold single buffer gl widget */
frameDB, /* frame to hold double buffer gl widget */
glwSB,
glwDB,
glw; /* current widget */
GLXContext glxc; /* current glx context */
Arg args[20];
int acnt;
void
main(int argc, char** argv)
{
XtToolkitInitialize();
appContext = XtCreateApplicationContext();
display = XtOpenDisplay(appContext, NULL, "Obuffer","obuffer",NULL,0,
&argc,argv);
if (!display) {
printf("%s : Unable to open display\n",argv[0]);
exit(0);
}
printf("\n---------------------------------------------\n");
printf("Demo to toggle single/double buffer mode \n\n");
printf("Press: left button for single buffer\n\
middle button for double buffer\n\
right button for animation\n");
quadObj = gluNewQuadric (); /* this will be used in drawScene */
createToplevel(); /* create widget hierarchy */
XtAppMainLoop(appContext);
}
void
createToplevel()
{
acnt = 0;
XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
XtSetArg(args[acnt],XmNminWidth, 300);acnt++;
XtSetArg(args[acnt],XmNminAspectX, 1);acnt++;
XtSetArg(args[acnt],XmNminAspectY, 1);acnt++;
XtSetArg(args[acnt],XmNmaxAspectX, 1);acnt++;
XtSetArg(args[acnt],XmNmaxAspectY, 1);acnt++;
toplevel = XtAppCreateShell("toplevel","obuffer",
applicationShellWidgetClass,
display,args,acnt);
/* create two drawing area widgets as children of
toplevel, only manage one at a time */
createDoubleBuffer();
createSingleBuffer();
/* realize widget hierarchy */
XtRealizeWidget(toplevel);
toDoubleBuffer(); /* let the default be double buffer */
}
void
createSingleBuffer()
{
acnt = 0;
frameSB = XtCreateWidget("frameSB", xmFrameWidgetClass,
toplevel, args, acnt);
/* NOTE : do not manage child here */
acnt = 0;
XtSetArg(args[acnt], GLwNrgba, TRUE); acnt++;
XtSetArg(args[acnt], GLwNdoublebuffer, FALSE); acnt++;
XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
glwSB = GLwCreateMDrawingArea(frameSB, "glwSB", args, acnt);
XtManageChild(glwSB);
/* register callbacks */
XtAddCallback(glwSB, GLwNexposeCallback, exposeCB, NULL);
XtAddCallback(glwSB, GLwNresizeCallback, resizeCB, NULL);
XtAddCallback(glwSB, GLwNginitCallback, initCB, NULL);
XtAddCallback(glwSB, GLwNinputCallback, inputCB, NULL);
}
void
createDoubleBuffer()
{
acnt = 0;
frameDB = XtCreateWidget("frameDB", xmFrameWidgetClass,
toplevel, args, acnt);
XtManageChild(frameDB);
acnt = 0;
XtSetArg(args[acnt], GLwNrgba, TRUE); acnt++;
XtSetArg(args[acnt], GLwNdoublebuffer, TRUE); acnt++; /* NOTE */
XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
glwDB = GLwCreateMDrawingArea(frameDB, "glwDB", args, acnt);
XtManageChild(glwDB);
/* register callbacks */
XtAddCallback(glwDB, GLwNexposeCallback, exposeCB, NULL);
XtAddCallback(glwDB, GLwNresizeCallback, resizeCB, NULL);
XtAddCallback(glwDB, GLwNginitCallback, initCB, NULL);
XtAddCallback(glwDB, GLwNinputCallback, inputCB, NULL);
}
void
toSingleBuffer(void)
{
acnt = 0;
XtSetArg(args[acnt],XmNtitle, "SINGLE buffer");acnt++;
XtSetValues(toplevel,args,acnt);
XtManageChild(frameSB);
XtUnmanageChild(frameDB);
glw = glwSB;
doubleBuffer = FALSE;
XtSetArg(args[0], XmNuserData, &glxc);
XtGetValues(glw, args, 1);
GLwDrawingAreaMakeCurrent(glw, glxc);
setMatrix();
drawScene();
}
void
toDoubleBuffer(void)
{
acnt = 0;
XtSetArg(args[acnt],XmNtitle, "DOUBLE buffer");acnt++;
XtSetValues(toplevel,args,acnt);
XtManageChild(frameDB);
XtUnmanageChild(frameSB);
glw = glwDB;
doubleBuffer = TRUE;
XtSetArg(args[0], XmNuserData, &glxc);
XtGetValues(glw, args, 1);
GLwDrawingAreaMakeCurrent(glw, glxc);
setMatrix();
drawScene();
}
void
drawScene(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
gluQuadricDrawStyle (quadObj, GLU_LINE);
glColor3f (0.7, 0.3, 1.0);
glTranslatef (12.0,12.0,0.0);
glRotatef(ax,1.0,0.0,0.0);
glRotatef(-ay,0.0, 1.0, 0.0);
gluCylinder (quadObj, 2.0,5.0,10.0,16,6);
glPopMatrix();
glPushMatrix();
gluQuadricDrawStyle (quadObj, GLU_FILL);
glColor3f (3.2, 0.8, 1.0);
glTranslatef (15.0,15.0,0.0);
glRotatef(ax,1.0,0.0,0.0);
glRotatef(ay,0.0, 1.0, 0.0);
glRotatef(az,0.0,0.0,1.0);
gluDisk(quadObj,2.0,5.0,16,4);
gluQuadricDrawStyle (quadObj, GLU_SILHOUETTE);
glColor3f (1.0, 0.2, 1.0);
glTranslatef (7.0,7.0,0.0);
glRotatef(-ax,1.0,0.0,0.0);
glRotatef(az,0.0,0.0,1.0);
gluPartialDisk (quadObj, 0.0,5.0,20,3,135.0,270.0);
glPopMatrix();
glFlush();
if (doubleBuffer) glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
}
void
setMatrix(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,32.0,0.0,32.0,-15.0,15.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void
animation(void)
{
register int i;
for (i=0;i<70;i++) {
ax += 5.0;
ay -= 2.0;
az += 5.0;
if (ax >= 3600) ax = 0.0;
if (ay <= -3600) ay = 0.0;
if (az >= 3600) az = 0.0;
drawScene();
}
}
void
inputCB(Widget w, XtPointer client_data, XtPointer call)
{
char buffer[1];
KeySym keysym;
GLwDrawingAreaCallbackStruct *call_data;
call_data = (GLwDrawingAreaCallbackStruct *) call;
switch(call_data->event->type) {
case ButtonPress:
switch (call_data->event->xbutton.button) {
case Button1:
toSingleBuffer();
break;
case Button2 :
toDoubleBuffer();
break;
case Button3 :
animation();
break;
}
break;
default:
break;
}
}
void
resizeCB(Widget w, XtPointer client_data, XtPointer call)
{
GLwDrawingAreaCallbackStruct *call_data;
call_data = (GLwDrawingAreaCallbackStruct *) call;
XtSetArg(args[0], XmNuserData, &glxc);
XtGetValues(w, args, 1);
GLwDrawingAreaMakeCurrent(w, glxc);
glViewport(0, 0, call_data->width, call_data->height);
drawScene();
}
void
exposeCB(Widget w, XtPointer client_data, XtPointer call)
{
GLwDrawingAreaCallbackStruct *call_data;
call_data = (GLwDrawingAreaCallbackStruct *) call;
XtSetArg(args[0], XmNuserData, &glxc);
XtGetValues(w, args, 1);
GLwDrawingAreaMakeCurrent(w, glxc);
drawScene();
}
void
initCB(Widget w, XtPointer client_data, XtPointer call)
{
XVisualInfo *vi;
XtSetArg(args[0], GLwNvisualInfo, &vi);
XtGetValues(w, args, 1);
glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
XtSetArg(args[0], XmNuserData, glxc);
XtSetValues(w, args, 1);
}